perf: Add static Engine.evaluate() and shared operator registry for bulk evaluation#432
Open
mridulbarman027 wants to merge 2 commits intoCacheControl:masterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When evaluating thousands of different condition sets against different facts using the same
default operators, users must create a
new Engine()per condition set. This is expensive due to:addRule()callrun()callThis results in ~20+ object allocations per evaluation. At scale (e.g. 250K evaluations),
this creates ~5M short-lived objects and significant GC pressure.
Solution
1. Shared default operator registry (singleton)
Object.freeze()d — created once at module loadOperatorMapgains astatic shared()method returning a singleton pre-loaded with all defaultsOperatorMapsupports aparentoption for prototype-chain-style delegationEngineinstance creates a lightweight childOperatorMapthat inherits from the sharedsingleton instead of cloning all 16 operator/decorator objects
2. Static
Engine.evaluate(conditions, facts, options?)methodA new lightweight evaluation path that bypasses all heavy infrastructure:
all/any/notnesting{ results, failureResults, events, failureEvents }allowUndefinedFacts,pathResolver,operatorMap3. Optimized Engine constructor
parent: OperatorMap.shared(), inheriting all defaultsvia delegation — zero per-instance operator allocations for the common case
Benchmark results (10K iterations)
new Engine() + addRule() + run() : 100.0ms (100,000 ops/sec)
Engine.evaluate() [static] : 34.4ms (290,908 ops/sec)
Reused engine.run() : 83.9ms (119,230 ops/sec)
Speedup (static vs new Engine) : ~2.9x
Changes
src/engine-default-operators.jssrc/engine-default-operator-decorators.jssrc/operator-map.jsstatic shared(), parent delegation (_hasOperator,_getOperator,_getDecorator)src/engine.jsstatic evaluate(), constructor uses shared parent OperatorMaptest/engine-evaluate.test.jsbenchmark/evaluate-benchmark.js